package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.mapper.SalaryMapper;
import com.xxxx.server.page.RespPageBean;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Salary;
import com.xxxx.server.pojo.vo.SalaryCfg;
import com.xxxx.server.service.ISalaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author brucejin
 * @since 2020-10-15
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private EmployeeMapper employeeMapper;



    /**
     * 查询薪资账套
     * @return
     */
    @Override
    public List<Salary> querySalary() {
        return salaryMapper.querySalary();
    }

    /**
     * 添加薪资账套
     * 参数：账套信息
     * 返回值：RespBean
     *  1. 账套名称  是否为空，是否已经存在
     *  2. 添加用户信息
     *  返回影响行数
     * @param salary
     * @return
     */
    @Override
    public RespBean addSalary(Salary salary) {
        if (salary.getName() == null){
            return RespBean.error("请输入账套名称！");
        }else{
            //mybatis-plus通过一个属性查询账套信息
            QueryWrapper<Salary> wrapper = new QueryWrapper<Salary>().eq("name", salary.getName());

            if (null != salaryMapper.selectOne(wrapper)) {
                return RespBean.error("薪资账套已存在！");
            }
            salary.setCreateDate(LocalDateTime.now(Clock.system(ZoneId.of("Asia/Shanghai"))));
            salary.setId(salary.getId());
            int i = salaryMapper.addSalary(salary);
            if (i == 1){
                return RespBean.success("添加成功");
            }else {
                return RespBean.error("添加失败");
            }
        }
    }

    /**
     * 修改薪资账套
     * @param salary
     * @return
     */

    @Override
    public RespBean updateSalary (Salary salary) {
        if (salary == null) {
            return RespBean.error("请输入账套名称");
        } else {
            Salary salaryByName = salaryMapper.querySalaryByName(salary.getName());
            salary.setCreateDate(LocalDateTime.now(Clock.system(ZoneId.of("Asia/Shanghai"))));
            salary.setId(salary.getId());
            int i;
            if(salaryByName == null){
                i = salaryMapper.updateSalary(salary);
                return getRespBeanWithUpte(i,"更改成功");
            }
            else if (!salaryByName.getId().equals(salary.getId()) && salaryByName!= null){
                return RespBean.error("该薪资账套已存在！");
            }
            else {
                i = salaryMapper.updateById(salary);
                return getRespBeanWithUpte(i,"更新成功啦");
            }
        }
    }

    private RespBean getRespBeanWithUpte(int i,String s){
        if(i ==1){
            return RespBean.success(s);
        }
        else {
            return RespBean.error("更改失败");
        }
    }

    /**
     * 删除薪资账套
     * @param id
     * @return
     */
    @Override
    public RespBean deleteSalaryById(Integer id) {
        if(id == null){
            return RespBean.error("请选择");
        }else {

            employeeMapper.updateEmployeeSalaryId(id);

            int i = salaryMapper.deleteSalaryById(id);

            if (i ==1){
                return  RespBean.success("删除成功");
            }else {
                return  RespBean.error("删除失败");
            }
        }
    }

    /**
     * 删除多个薪资账套
     * @param ids
     * @return
     */
    @Override
    public RespBean deleteSalaryByIds(String[] ids) {
        if (ids == null || ids.length ==0){
            return RespBean.error("请选择");
        }else {

            employeeMapper.updateEmploySalaryIds(ids);

            int i = salaryMapper.deleteSalaryByIds(ids);
            if (i ==1){
                return  RespBean.success("删除成功");
            }else {
                return  RespBean.error("删除失败");
            }
        }
    }

    /**
     * 获取员工账套
     * @param currentPage
     * @param size
     * @param salaryCfg
     * @return
     */
    @Override
    public RespPageBean queryAllSalaryCfg(Integer currentPage, Integer size, SalaryCfg salaryCfg) {
        if (currentPage != null && size != null){
            currentPage = (currentPage -1) * size;
        }
        List<SalaryCfg> data = salaryMapper.queryAllSalaryCfg(currentPage,size,salaryCfg);

        Long total = salaryMapper.getTotal(salaryCfg);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    /**
     * 修改员工账套
     * @param eid
     * @param sid
     * @return
     */
    @Override
    public int updateSalaryCfg(Integer eid, Integer sid) {
        return salaryMapper.updateSalaryCfg(eid, sid);
    }
}
